﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PKURG.CostSplit.Data;
using PKURG.CostSplit.Entities;
using PKURG.CostSplit.Business.QueryBase;
namespace PKURG.CostSplit.Business
{
    public class Relation
    {

        public static List<dynamic> GetERPProjectList()
        {
            return ExecuteQuery.GetList(
                new QueryBase.QueryInfo()
                {
                    ProcName = "usp_GetEProjectList"
                });
        }



        public static List<dynamic> GetRelationMProjectList(int eProjectId)
        {
            return ExecuteQuery.GetList(
                new QueryBase.QueryInfo()
                {
                    ProcName = "usp_GetRelationMProjectList",
                    Params = { eProjectId }
                });
        }

        public static List<dynamic> GetNoRelationMProjectList(int eProjectId)
        {
            return ExecuteQuery.GetList(
                new QueryBase.QueryInfo()
                {
                    ProcName = "usp_GetNoRelationMProjectList",
                    Params = { eProjectId }
                });
        }

        public static bool Relation_Project(int eProjectId, Guid mProjectId, string userName)
        {
            Services.RsProjectService service = new Services.RsProjectService();
            return service.Insert(new RsProject()
             {
                 EProject = eProjectId,
                 MProject = mProjectId,
                 CreateTime = DateTime.Now,
                 CreateUser = userName
             });
        }

        public static void UnRelation_Project(int eProjectId, Guid mProjectId)
        {
            Services.RsProjectService service = new Services.RsProjectService();
            TList<RsProject> infos = service.Find(string.Format("E_Project='{0}' and M_Project='{1}'", eProjectId, mProjectId));
            service.Delete(infos);
        }



        public static List<dynamic> GetERPProjectStageList(int projectStageId)
        {
            return ExecuteQuery.GetList(
                new QueryBase.QueryInfo()
                {
                    ProcName = "usp_GetERPProjectStageList",
                    Params = { projectStageId }
                });
        }

        public static List<dynamic> GetRelationMProjectStageList(int eProjectStageId)
        {
            return ExecuteQuery.GetList(
                new QueryBase.QueryInfo()
                {
                    ProcName = "usp_GetRelationMProjectStageList",
                    Params = { eProjectStageId }
                });
        }

        public static List<dynamic> GetNoRelationMProjectStageList(int eProjectStageId)
        {
            return ExecuteQuery.GetList(
                new QueryBase.QueryInfo()
                {
                    ProcName = "usp_GetNoRelationMProjectStageList",
                    Params = { eProjectStageId }
                });
        }

        public static bool Relation_ProjectStage(int eProjectStageId, Guid mProjectStageId, string userName)
        {
            Services.RsProjectStageService service = new Services.RsProjectStageService();
            return service.Insert(new RsProjectStage()
            {
                EProjectStage = eProjectStageId,
                MProjectStage = mProjectStageId,
                CreateTime = DateTime.Now,
                CreateUser = userName
            });
        }

        public static void UnRelation_ProjectStage(int eProjectStageId, Guid mProjectStageId)
        {
            Services.RsProjectStageService service = new Services.RsProjectStageService();
            TList<RsProjectStage> infos = service.Find(string.Format("{0} && ='{1}'", eProjectStageId, mProjectStageId));
            service.Delete(infos);
        }


        public static List<dynamic> GetDevProjectList()
        {
            return ExecuteQuery.GetList(
                new QueryBase.QueryInfo()
                {
                    ProcName = "usp_n_GetDevProjectList"
                });
        }
        public static List<dynamic> GetDevProjectStageList(string projectStageId)
        {
            return ExecuteQuery.GetList(
                new QueryBase.QueryInfo()
                {
                    ProcName = "usp_n_GetDevProjectStageList",
                    Params = { projectStageId }
                });
        }

        public static List<dynamic> GetRelationList(string id,int rType)
        {
            return ExecuteQuery.GetList(
                new QueryBase.QueryInfo()
                {
                    ProcName = "usp_n_GetRelationList",
                    Params = { id, rType}
                });
        }

        public static List<dynamic> GetNoRelationList(string id,int rType)
        {
            return ExecuteQuery.GetList(
                new QueryBase.QueryInfo()
                {
                    ProcName = "usp_n_GetUnRelationList",
                    Params = { id, rType }
                });
        }

        //public static bool Relation_ProjectStage(int eProjectStageId, Guid mProjectStageId, string userName)
        //{
        //    Services.RsProjectStageService service = new Services.RsProjectStageService();
        //    return service.Insert(new RsProjectStage()
        //    {
        //        EProjectStage = eProjectStageId,
        //        MProjectStage = mProjectStageId,
        //        CreateTime = DateTime.Now,
        //        CreateUser = userName
        //    });
        //}

        //public static void UnRelation_ProjectStage(int eProjectStageId, Guid mProjectStageId)
        //{
        //    Services.RsProjectStageService service = new Services.RsProjectStageService();
        //    TList<RsProjectStage> infos = service.Find(string.Format("{0} && ='{1}'", eProjectStageId, mProjectStageId));
        //    service.Delete(infos);
        //}

        public static int Relate(string leftId, string rightId, string userName, int rType)
        {
            return QueryBase.CustomProcess.ExecuteProcNonQuery("usp_n_Relate",new Object[] 
            { leftId, rightId, userName, (int)rType });
        }

        public static int UnRelate(string leftId, string rightId, int rType)
        {
            return QueryBase.CustomProcess.ExecuteProcNonQuery("usp_n_UnRelate", new Object[] 
            { leftId, rightId, (int)rType });
        }
    }
}
